<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>登录</title>
    <script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>
    <script src="https://unpkg.com/vue-router@3.0.0/dist/vue-router.js"></script>
    <!-- 引入样式 -->
    <link rel="stylesheet" href="https://unpkg.com/element-ui/lib/theme-chalk/index.css">
    <!-- 引入组件库 -->
    <script src="https://unpkg.com/element-ui/lib/index.js"></script>
</head>
<body>
<div id="app">
    <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
        <el-form-item label="账号" prop="username">
            <el-input v-model="ruleForm.username"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
            <el-input v-model="ruleForm.password"></el-input>
        </el-form-item>
        <el-form-item label="验证码" prop="define">
            <el-input v-model="ruleForm.define"></el-input>
        </el-form-item>
        <img :src="codeUrl" @click="refreshCode"><a href="#" id="change"
                                                    @click="refreshCode">看不清？</a>
        <el-form-item>
            <el-button type="primary" @click="submitForm('ruleForm')">立即登录</el-button>
            <el-button @click="sign">注册</el-button>
            <el-button @click="recover">找回密码</el-button>
            <el-button @click="resetForm('ruleForm')">重置</el-button>
        </el-form-item>
    </el-form>
</div>

<script src="js/axios-0.18.0.js"></script>
<script>
    new Vue({
        el: "#app",
        data() {
            return {
                user: {
                    id: '',
                    username: '',
                    password: '',
                    phone: '',
                    email: '',
                    headImage: ''
                },
                ruleForm: {
                    username: '',
                    password: '',
                    define: ''
                },
                codeUrl: '',
                reCode: '',
                rules: {
                    username: [
                        {required: true, message: '请输入账号', trigger: 'blur'}
                    ],
                    password: [{required: true, message: '请输入密码', trigger: 'blur'}
                    ],
                    define: [{required: true, message: '请输入验证码', trigger: 'blur'}]
                }
            };
        },
        mounted() {
            this.refreshCode();
        },
        methods: {
            refreshCode() {
                var _this = this;
                axios({
                    method: "get",
                    url: "http://localhost:8080/webdemo/method/code/imageCode",
                    responseType: 'blob'
                }).then(function (resp) {
                    _this.codeUrl = URL.createObjectURL(new Blob([resp.data]));
                });
            },
            submitForm(formName) {
                var _this = this;
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        axios({
                            method: "post",
                            url: "http://localhost:8080/webdemo/method/user/logIn",
                            data: {
                                username: _this.ruleForm.username,
                                password: _this.ruleForm.password,
                                define: _this.ruleForm.define
                            }
                        }).then(function (resp) {
                            if (resp.data.msg === "success") {
                                _this.user = resp.data.data[0];
                                location.href = "menu.html?userId=" + JSON.stringify(_this.user.id);
                            } else if (resp.data.msg === 'worseCode') {
                                alert("验证码错误");
                            } else {
                                alert("用户名或密码错误");
                            }
                        })
                    } else {
                        console.log('error submit!!');
                        return false;
                    }
                });
            },
            resetForm(formName) {
                this.$refs[formName].resetFields();
            },
            sign() {
                location.href = "SignIn.html"
            },
            recover() {
                location.href = 'recoverPassword.html'
            }
        }
    });
</script>
</body>
</html>